﻿using System.Collections.Generic;
using ImageTagToolViewModel.Interfaces;

namespace ImageTagToolViewModel.Models
{
    /// <summary>
    /// A class that manages core runtime image objects.
    /// </summary>
    public class PictureHandler : IMemoryUsageApproximable
    {
        private static PictureHandler _Instance;

        /// <summary>
        /// Gets the PictureHandler instance.
        /// </summary>
        public static PictureHandler Instance
        {
            get
            {
                object locker = new object();
                lock (locker)
                {
                    if (_Instance == null)
                        _Instance = new PictureHandler();
                }
                return _Instance;
            }
        }
        /// <summary>
        /// Gets the Pictures' list.
        /// </summary>
        public List<Picture> Pictures {get; private set;}

        /// <summary>
        /// Gets or sets the index of the actual image in the Pictures list.
        /// </summary>
        internal int ActualPictureIndex { get; set; }

        /// <summary>
        /// Gets the actual picture
        /// </summary>
        public Picture ActualPicture
        {
            get
            {
                if (ActualPictureIndex == -1)
                    return null;
                return Pictures[ActualPictureIndex];
            }
        }

        private PictureHandler()
        {
            Pictures = new List<Picture>();
            ActualPictureIndex = -1;
        }

        /// <summary>
        /// Tries to approximate the stored data in this PictureHandler object.
        /// </summary>
        /// <returns>The amount of data in Bytes</returns>
        public int ApproximateMemoryUsage()
        {
            int memory = 0;
            foreach (var p in Pictures)
                memory += p.ApproximateMemoryUsage();
            return memory;
        }

        /// <summary>
        /// Clears all data from this object.
        /// </summary>
        public void Clear()
        {
            ActualPictureIndex = -1;
            Pictures.Clear();
        }
    }
}
